package com.xjnt.base.support.admin.service;

import java.util.List;

import com.xjnt.base.support.admin.entity.Menu;
import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.sql.SqlKit;
import com.xjnt.frameworks.plugin.sql.criterion.Restrictions;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;
import com.xjnt.frameworks.plugin.sql.criterion.expression.Order;

/**
 * @author caoyong
 *
 */
@Service
public class MenuService extends BaseService<Menu> {

	@Override
	public Menu getModel() {
		return Menu.me;
	}
	/**
	 * 根据权限ID获取所有模块和菜单
	 * @param permId
	 * @return
	 */
	public List<Menu> findByPermId(String permId){
		SqlCriteria criteria = new SqlCriteria(getModel(), "menu");
		criteria.addRelation("sys_permission_menu", "spm");
		criteria.addRelation(Permission.me, "perm");
		criteria.add(Restrictions.eqProp("menu.uuid", "spm.menuid"));
		criteria.add(Restrictions.eqProp("perm.uuid", "spm.permissionid"));
		criteria.add(Restrictions.eq("perm.uuid", permId));
		criteria.addOrder(Order.asc(Menu.ORDER));
		return getModel().find(criteria.toSql(), permId);
	}
	
	/**
	 * 根据权限ID列表获取所有模块和菜单
	 * @param permIds
	 * @return
	 */
	public List<Menu> findByPermIds(Object[] permIds){
		SqlCriteria criteria = new SqlCriteria(getModel(), "menu");
		criteria.addRelation("sys_permission_menu", "spm");
		criteria.addRelation(Permission.me, "perm");
		criteria.add(Restrictions.eqProp("menu.uuid", "spm.menuid"));
		criteria.add(Restrictions.eqProp("perm.uuid", "spm.permissionid"));
		criteria.add(Restrictions.in("perm.uuid", permIds));
		criteria.addOrder(Order.asc(Menu.TYPE));
		criteria.addOrder(Order.asc(Menu.ORDER));
		return getModel().find(criteria.toSql(), permIds);
	}
	
	/**
	 * 根据权限ID和平台ID获取模块
	 * @param permId
	 * @param platformId
	 * @return
	 */
	public List<Menu> findModuleByPermId(String permId, String platformId){
		SqlCriteria criteria = new SqlCriteria(getModel(), "menu");
		criteria.addRelation("sys_permission_menu", "spm");
		criteria.addRelation(Permission.me, "perm");
		criteria.add(Restrictions.eqProp("menu.uuid", "spm.menuid"));
		criteria.add(Restrictions.eqProp("perm.uuid", "spm.permissionid"));
		criteria.add(Restrictions.eq("menu.type", 0));
		criteria.add(Restrictions.eq("perm.uuid", permId));
		criteria.addOrder(Order.asc(Menu.ORDER));
		return getModel().find(criteria.toSql(), permId);
	}
	
	/**
	 * 根据权限ID和模块ID获取菜单
	 * @param permId
	 * @param moduleId
	 * @return
	 */
	public List<Menu> findMenuByPermId(String permId, String moduleId){
		SqlCriteria criteria = new SqlCriteria(getModel(), "menu");
		criteria.addRelation("sys_permission_menu", "spm");
		criteria.addRelation(Permission.me, "perm");
		criteria.add(Restrictions.eqProp("menu.uuid", "spm.menuid"));
		criteria.add(Restrictions.eqProp("perm.uuid", "spm.permissionid"));
		criteria.add(Restrictions.eq("menu.type", 1));
		criteria.add(Restrictions.eq("perm.uuid", permId));
		criteria.addOrder(Order.asc(Menu.ORDER));
		return getModel().find(criteria.toSql(), permId);
	}
	
	public List<Menu> findByType(Integer type){
		return getModel().find(SqlKit.findSql("Menu.findByType"), type);
	}
	
	public List<Menu> notAllocModule(){
		return getModel().find(SqlKit.findSql("Menu.notAllocModule"));
	}
	
	public List<Menu> notAllocMenu(){
		return getModel().find(SqlKit.findSql("Menu.notAllocMenu"));
	}
}
